package com.king.java.tools.http.entity;


import com.king.java.tools.utils.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 描述:
 *
 * @author : sujinchen
 * @date : 2020/10/15
 */
public class ConnectEntity {
    /**
     * 请求行
     * method url protocol
     */
    private String reqLine;
    /**
     * 请求头
     */
    private Map<String, String> reqHeaders=new HashMap<>();
    /**
     * 请求体
     */
    private String reqBody;


    /**
     * 响应码
     * protocol code message
     */
    private String resLine="";
    /**
     * 响应头
     */
    private Map<String, String> resHeaders=new HashMap<>();
    /**
     * 响应体
     */
    private String resBody;

    private Date startTime=new Date();

    private Date endTime=new Date();

    private Integer reactionTime = 0;


    public String getReqLine() {
        return reqLine;
    }

    public void setReqLine(String reqLine) {
        if (StringUtils.isEmpty(reqLine)) {
            this.reqLine = "";
        } else {
            this.reqLine = reqLine;
        }
    }

    public Map<String, String> getReqHeaders() {
        return reqHeaders;
    }

    public void setReqHeaders(Map<String, String> reqHeaders) {
        if (reqHeaders != null) {
            this.reqHeaders = reqHeaders;

        }
    }

    public String getReqBody() {
        return reqBody;
    }

    public void setReqBody(String reqBody) {
        if (StringUtils.isEmpty(reqBody)) {
            this.reqBody = "";
        } else {
            this.reqBody = reqBody;
        }
    }

    public String getResLine() {
        return resLine;
    }

    public void setResLine(String resLine) {
        if (StringUtils.isEmpty(resLine)) {
            this.resLine = "";
        } else {
            this.resLine = resLine;
        }
    }

    public Map<String, String> getResHeaders() {
        return resHeaders;
    }

    public void setResHeaders(Map<String, String> resHeaders) {
        if (resHeaders != null) {
            this.resHeaders = resHeaders;
        }

    }

    public String getResBody() {
        return resBody;
    }

    public void setResBody(String resBody) {
        if (StringUtils.isEmpty(resBody)) {
            this.resBody = "";
        } else {
            this.resBody = resBody;
        }
    }

    public Date getStartTime() {
        return startTime;
    }

    public void setStartTime(Date startTime) {
        this.startTime = startTime;
    }

    public Date getEndTime() {
        return endTime;
    }

    public void setEndTime(Date endTime) {
        this.endTime = endTime;
    }

    public Integer getReactionTime() {
        return reactionTime;
    }

    public void setReactionTime(Integer reactionTime) {
        this.reactionTime = reactionTime;
    }

    @Override
    public String toString() {
        StringBuffer result = new StringBuffer();
        SimpleDateFormat format=new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss:SSS");
        result.append("【开始日期】：" + format.format(startTime)+"\n");
        result.append("【结束日期】：" + format.format(endTime)+"\n");
        result.append("【响应时长】：" + reactionTime+" ms\n");
        join(result,reqLine,reqHeaders,reqBody);
        result.append("\n--------------------------------------------\n");
        join(result,resLine,resHeaders,resBody);
        return result.toString();
    }

    private void join(StringBuffer result,String line,Map<String, String> headers,String body) {
        result.append(line + "\n");
        for (Map.Entry map :
                headers.entrySet()) {
            result.append(map.getKey() + ":" + map.getValue() + "\n");
        }
        result.append("\n"+body + "\n");
    }
}
